package com.zzyl.serve.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;

import com.zzyl.common.core.domain.R;
import com.zzyl.framework.config.IotClientConfig;
import com.zzyl.serve.dto.DeviceDto;
import com.zzyl.serve.vo.DeviceDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.serve.domain.Device;
import com.zzyl.serve.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 * 
 * @author koi
 * @date 2025-05-10
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备管理相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private IotClientConfig iotClientConfig;

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备管理列表")
    public TableDataInfo<List<Device>> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出设备管理列表")
    public void export(HttpServletResponse response, Device device)
    {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
//    @GetMapping(value = "/{id}")
//    @ApiOperation("获取设备管理详细信息")
//    public R<Device> getInfo(@ApiParam(value = "设备管理ID", required = true)
//            @PathVariable("id") Long id)
//    {
//        return R.ok(deviceService.selectDeviceById(id));
//    }

    /**
     * 新增设备管理
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增设备管理")
    public AjaxResult add(@ApiParam(value = "设备管理实体", required = true) @RequestBody Device device)
    {
        return toAjax(deviceService.insertDevice(device));
    }

    /**
     * 修改设备管理
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改设备管理")
    public AjaxResult edit(@ApiParam(value = "设备管理实体", required = true) @RequestBody Device device)
    {
        return toAjax(deviceService.updateDevice(device));
    }

    /**
     * 删除设备管理
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除设备管理")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids)
    {
        return toAjax(deviceService.deleteDeviceByIds(ids));
    }

    @PostMapping("/syncProductList")
    @ApiOperation(value ="同步产品列表")
    public AjaxResult syncProductList(){
        deviceService.syncProductList();
        return success();
    }


    @GetMapping("/allProduct")
    @ApiOperation(value ="获取所有产品列表")
    public AjaxResult allProduct(){
        return success(deviceService.allProduct());
    }

    /**
     * 注册设备
     * 接口地址:/nursing/device/register
     * 请求方式:POST
     */
    @PostMapping("/register")
    @ApiOperation(value ="注册设备")
    public AjaxResult regiterDevice(@RequestBody DeviceDto deviceDto){
        deviceService.registerDevice(deviceDto);
        return success();
    }

    /**
     * 3.5.7.2 接口说明：
     * 接口地址:/nursing/device/{iotId}
     * 请求方式:GET
     */

    @GetMapping("/{iotId}")
    @ApiOperation(value ="上报设备")
    public AjaxResult showDeviceInfo(@PathVariable String iotId)
    {
        DeviceDetailVo vo =deviceService.showDeviceInfo(iotId);
        return success(vo);
    }
    /**
     * 查询上报数据
     */
//    @GetMapping("/queryServiceProperties/{iotId}")
//    @ApiOperation(value ="查询上报数据")
//    public AjaxResult queryServiceProperties(@PathVariable String iotId) {
//         List<Map> list = deviceService.queryServiceProperties(iotId);
//         return AjaxResult.success(list);
//    }
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation(value = "查看设备上报的数据")
    public AjaxResult queryServiceProperties(@PathVariable String iotId)
    {

        // 调用service进行业务处理
        List<Map> list = deviceService.queryServiceProperties(iotId);

        // 返回结果
        return AjaxResult.success(list);
    }
}
